﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Tzync
{
    public partial class FilterUIForm : Form
    {
        private SyncTool syncTool;
        private List<String> filterList, fileFilterList;
        private bool filterFlag;    //toggle include/exclude
        FiltersTreeViewUIForm filterForm;
        private bool selectAll; //toggle select/de-select

        public FilterUIForm(SyncTool newSyncTool, List<string> filters, List<string> advancedFilters)
        {
            this.fileFilterList = new List<string>(filters);
            this.filterList = new List<string>(advancedFilters);
            filterFlag = true;
            syncTool = newSyncTool;
            InitializeComponent();
            DisplayFilters();               
        }

        private void FilterUIForm_Load(object sender, EventArgs e)
        {
            filterCB.SelectedIndex = 0;
            infoLabel.Text = "";

            applyBtnToolTip.SetToolTip(applyBtn, "Click here to apply the filters");
            addBtnToolTip.SetToolTip(addBtn, "Click here to add the selected file or folder to the list of filters");
            selectBtnToolTip.SetToolTip(selectAllBtn, "Click here to select or de-select all filters");
            removeBtnToolTip.SetToolTip(removeBtn, "Click here to remove selected filters");
            treeViewBtnToolTip.SetToolTip(treeViewBtn, "Click here to filter files/folders in tree view");
            cancelBtnToolTip.SetToolTip(cancelBtn, "Click here to cancel and return to the previous screen");
        }

        private void FilterUIForm_Shown(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            filterTB.Focus();
            DisplayFilters();
        }

        private void FilterUIForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = DialogResult.OK;
        }

        private void filterCB_SelectionChangeCommitted(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (filterCB.SelectedIndex == 0)
                filterFlag = true;
            else
                filterFlag = false;
        }

        private void FilterUIForm_DragEnter(object sender, DragEventArgs e)
        {
            infoLabel.Text = "";
            //only allow files/folders to be dropped here
            if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
                e.Effect = DragDropEffects.All;
        }

        private void FilterUIForm_DragDrop(object sender, DragEventArgs e)
        {
            infoLabel.Text = "";
            string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);

            foreach (String s in files)
            {
                if (s.Contains(syncTool.GetRootAddress()) && !filterList.Contains(s))
                    filterList.Add(s);
            }

            DisplayFilters();
        }

        private void addBtn_Click(object sender, EventArgs e)
        {
            if (filterTB.Text == "")
                infoLabel.Text = "Please enter a valid file to filter!";
            else
            {
                fileFilterList.Add(filterTB.Text);
                filterTB.Clear();
                infoLabel.Text = "Filter added!";
                filterTB.Focus();
                DisplayFilters();
            }
        }

        private void applyBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            //do actual filtering
            
            syncTool.IncludeFlag(filterFlag);
            foreach (String s in fileFilterList)
                syncTool.AddFileFilter(s);
            foreach (String s in filterList)
                syncTool.AddAdvancedFilter(s);
            this.Close();
        }

        //displays added filters
        private void DisplayFilters()
        {
            ListViewItem filterItem;          
            filterLV.Items.Clear();
            
            for (int i = 0; i < fileFilterList.Count; i++)
            {
                filterItem = new ListViewItem();
                filterItem.Text = fileFilterList[i];
                filterLV.Items.Add(filterItem);
            }
           
            for (int i = 0; i < filterList.Count; i++)  
            {
                filterItem = new ListViewItem();
                filterItem.Text = filterList[i];
                filterLV.Items.Add(filterItem);
            }

            ResizeColumns();
        }

        //auto resize columns
        private void ResizeColumns()
        {
            int contentWidth, headerWidth;
            ColumnHeaderAutoResizeStyle headerResize;

            for (int index = 0; index < filterLV.Columns.Count; index++)
            {
                ColumnHeader header = filterLV.Columns[index];
                header.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                contentWidth = header.Width;

                header.AutoResize(ColumnHeaderAutoResizeStyle.HeaderSize);
                headerWidth = header.Width;

                if (contentWidth > headerWidth)
                    headerResize = ColumnHeaderAutoResizeStyle.ColumnContent;
                else
                    headerResize = ColumnHeaderAutoResizeStyle.HeaderSize;

                header.AutoResize(headerResize);
            }
        }

        private void treeViewBtn_Click(object sender, EventArgs e)
        {
            filterForm = new FiltersTreeViewUIForm(syncTool, ref filterList);
            filterForm.ShowDialog();
            DisplayFilters();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void selectAllBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (filterLV.Items.Count > 0)
            {
                if (!selectAll)
                {
                    foreach (ListViewItem item in filterLV.Items)
                        item.Checked = true;
                    selectAll = true;
                }
                else
                {
                    foreach (ListViewItem item in filterLV.Items)
                        item.Checked = false;
                    selectAll = false;
                }
            }
        }

        private void removeBtn_Click(object sender, EventArgs e)
        {
            infoLabel.Text = "";
            if (filterLV.CheckedItems.Count > 0)
            {
                for (int i = 0; i < filterLV.CheckedItems.Count; i++)
                {
                    filterList.Remove(filterLV.CheckedItems[i].Text);
                    fileFilterList.Remove(filterLV.CheckedItems[i].Text);
                }
            }
            DisplayFilters();
            filterTB.Focus();
            infoLabel.Text = "Selected filters have been removed!";
        }

        private void filterTB_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)13)
            {
                addBtn_Click(sender, e);
                filterTB.Clear();
                filterTB.Focus();
            }
        }
    }
}
